//
//  SDVideoVoiceFollowViewController.m
//  XNSudai
//
//  Created by Carlson Lee on 2017/4/5.
//  Copyright © 2017年 xulu. All rights reserved.
//

#import "SDVideoVoiceFollowViewController.h"
#import "VoiceFollowView.h"
#import "VideoRecordView.h"
#import "SDVoiceTipView.h"
#import "VoiceFollowMaskView.h"
#import "VideoBtnView.h"
#import "VideoPlayerView.h"

@interface SDVideoVoiceFollowViewController ()<AVSpeechSynthesizerDelegate>

@property(nonatomic, strong)VoiceFollowView* voiceView;//文字view
@property(nonatomic, strong)VideoRecordView* recordView;//录像view
@property(nonatomic, strong)VoiceFollowMaskView* maskView;//遮罩层
@property(nonatomic, strong)SDVoiceTipView* tipView;//提示view
@property(nonatomic, strong)VideoBtnView* btnView;//按钮
@property(nonatomic, strong)VideoPlayerView* playView;//播放view

@property(nonatomic, strong)AVSpeechSynthesizer* speech;//语音

@end

@implementation SDVideoVoiceFollowViewController

- (VoiceFollowView *)voiceView
{
    if(!_voiceView){
        _voiceView = [[VoiceFollowView alloc]initWithColor:UIColorFromRGB(0x3c3342) changeColor:[UIColor redColor]];
        [self.view addSubview:_voiceView];
        [_voiceView setBackgroundColor:AlphaColorWithRGBA(0xf2f0f5, .6)];
    }
    return _voiceView;
}

- (VideoRecordView *)recordView
{
    if(!_recordView){
        _recordView = [[VideoRecordView alloc]init];
        [self.view addSubview:_recordView];
    }
    return _recordView;
}

- (VoiceFollowMaskView *)maskView
{
    if(!_maskView){
        _maskView = [[VoiceFollowMaskView alloc]init];
        [self.view addSubview:_maskView];
    }
    return _maskView;
}

- (SDVoiceTipView *)tipView
{
    if(!_tipView){
        _tipView = [[SDVoiceTipView alloc]initWithType:TIP_TYPE_IMG_STR direction:TIP_DERECTION_HOR font:SDFont(12, FONT_TYPE_REGULAR)];
        [self.view addSubview:_tipView];
        [_tipView setBackgroundColor:AlphaColorWithRGBA(0x000000, .5)];
        [_tipView.tipLab setTextColor:UIColorFromRGB(0x999999)];
        _tipView.imgStr = @"word_tip";
        _tipView.tipStr = @"文字读错、画面不清晰将导致贷款申请失败";
    }
    return _tipView;
}

- (VideoBtnView *)btnView
{
    if(!_btnView){
        _btnView = [[VideoBtnView alloc]initWithBtnType:BTN_TYPE_READY];
        [self.view addSubview:_btnView];
    }
    return _btnView;
}

- (VideoPlayerView *)playView
{
    if(!_playView){
        _playView = [[VideoPlayerView alloc]init];
        [self.view addSubview:_playView];
    }
    return _playView;
}

- (AVSpeechSynthesizer *)speech
{
    if(!_speech){
        _speech = [[AVSpeechSynthesizer alloc] init];
        _speech.delegate = self;
    }
    return _speech;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"请大声朗读一下文字";
    
    [self setUp];
}

- (void)setUp
{
    __block NSString* voice = @"本人自愿向中银消费申请贷款，提供真实资料，遵守合约，同意上报征信";
    
    self.voiceView.frame = CGRectMake((UIFrameW(self.view)-CELL_WIDTH*11)/2, 64+10, CELL_WIDTH*11, CELL_WIDTH*3);
    self.voiceView.voiceStr = voice;
    
    self.playView.frame = CGRectMake(0, CGRectGetMaxY(self.voiceView.frame)+10, mainWidth, mainHeight-CGRectGetMaxY(self.voiceView.frame)-10);
    self.recordView.frame = self.playView.frame;
    
    self.maskView.frame = self.recordView.frame;
    self.tipView.frame = CGRectMake(0, CGRectGetMinY(self.recordView.frame), mainWidth, 30*ScaleX);
    self.btnView.frame = CGRectMake(0, UIFrameH(self.view)-64*ScaleX, UIFrameW(self.view), 44*ScaleX);
    __weak typeof(self) weakSelf = self;
    self.btnView.resBlock = ^(BTN_TYPE type, NSInteger idx){//0👈按钮，1👉按钮
        if(type==BTN_TYPE_READY){
            if(idx){
                [weakSelf followSpeakWithStr:voice];
                //[weakSelf.recordView startRecord:YES];
            }
        }else if (type==BTN_TYPE_FOLLOW){
            if(!idx && weakSelf.speech.isSpeaking){
                [weakSelf.speech stopSpeakingAtBoundary:AVSpeechBoundaryWord];
            }
        }else{
            if(!idx){
                [weakSelf voiceComplete:NO];
            }else{
                
            }
        }
    };
}

- (void)followSpeakWithStr:(NSString* )voiceStr
{
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:voiceStr];
    utterance.rate = 5.0;
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    if(!voice)return;
    utterance.voice = voice;
//    [self.speech speakUtterance:utterance];
}

- (void)voiceComplete:(BOOL )complete
{
    [self.btnView setAlpha:0.0];
    
    if(complete){
        self.title = @"请确认朗读内容是否准确清晰";
        self.btnView.type = BTN_TYPE_OVER;
        self.btnView.frame = self.voiceView.frame;
        [self.recordView openSession:NO];
        NSString *path = [NSTemporaryDirectory() stringByAppendingString:@"videoInfo.mov"];
        [self.playView loadWithPath:path];
        
        
        [UIView animateWithDuration:.25 animations:^{
            [self.voiceView setAlpha:0.0];
            [self.maskView setAlpha:0.0];
            [self.btnView setAlpha:1.0];
        }];
    }else{
        self.title = @"请大声朗读以下文字";
        self.btnView.type = BTN_TYPE_READY;
        self.btnView.frame = CGRectMake(0, UIFrameH(self.view)-64*ScaleX, UIFrameW(self.view), 44*ScaleX);
        [self.recordView openSession:YES];
        [UIView animateWithDuration:.25 animations:^{
            [self.voiceView setAlpha:1.0];
            [self.maskView setAlpha:1.0];
            [self.btnView setAlpha:1.0];
        }];
    }
}

#pragma mark----------AVSpeechSynthesizerDelegate

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
    self.btnView.type = BTN_TYPE_FOLLOW;
    
    //开始录制
    [self.recordView startRecord:YES];
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self voiceComplete:YES];
    });
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
    self.btnView.type = BTN_TYPE_READY;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
